﻿/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

using System;
using System.Collections;
using System.Collections.Generic;

namespace Gk3dEngine
{
    public class GameObject : Object
    {
        private TransformComponent _transform = null;
        private List<Component> _components=new List<Component>();
        
        private List<GameObject> _children = new List<GameObject>();

        public GameObject()
        {
            _id = InternalCall.createGameObject();//this function will create inner gameobject
        }

        internal GameObject(int id)
        {
            _id = id;
            InternalCall.OnCreateGameObject(id);//have id gameobjecct already created 
        }

        internal void addComponent(Component component)
        {
            if (component == null)
            {
                return;
            }
            
            if (component is TransformComponent)
            {
                _transform = (TransformComponent)component;
            }
            else
            {
                if (component != null)
                {
                    _components.Add(component);
                }
            }
        }

        internal TransformComponent getTransformComponent()
        {
            return _transform;
        }

        internal void update(uint deltaT)
        {
            int size = _components.Count;
            for (int i = 0; i < size; i++)
            {
                if (_components[i] != null)
                {
                    _components[i].update(deltaT);
                }
            }
            size = _children.Count;
            for (int i = 0; i < size; i++)
            {
                if (_children[i] != null)
                {
                    _children[i].update(deltaT);
                }
            }
        }

        internal void addChild(GameObject child)
        {
            if (child != null)
            {
                _children.Add(child);
            }        
        }
    }
}
